package com.jinwei.qunguanjia.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.system.dao.SysLoginLogDAO;
import com.jinwei.qunguanjia.system.dto.SysLoginLogDTO;
import com.jinwei.qunguanjia.system.po.SysLoginLogPO;
import com.jinwei.qunguanjia.common.constant.Constants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.common.utils.ip.AddressUtils;
import com.jinwei.qunguanjia.common.utils.ip.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 登录日志 服务层实现类
 * @author yuanzs
 */
@Service("sysLoginLogService")
public class SysLoginLogServiceImpl implements SysLoginLogService {

    @Resource
    private SysLoginLogDAO sysLoginLogDAO;

    @Override
    @Async
    public void save(SysLoginLogDTO sysLoginLogDTO, HttpServletRequest request) {
        SysLoginLogPO sysLoginLogPO = new SysLoginLogPO();
        BeanUtils.copyProperties(sysLoginLogDTO, sysLoginLogPO);
        sysLoginLogPO.setLoginId(IdUtils.getWorkerId());
        String ip = IpUtils.getIpAddr(request);
        sysLoginLogPO.setIpAddress(ip);
        String address = AddressUtils.getRealAddressByIP(ip);
        sysLoginLogPO.setLoginLocation(address);
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        // 获取客户端浏览器
        sysLoginLogPO.setBrowser(userAgent.getOperatingSystem().getName());
        // 获取客户端操作系统
        sysLoginLogPO.setOs(userAgent.getBrowser().getName());
        // 日志状态
        if (StringUtils.equalsAny(sysLoginLogDTO.getLoginStatus(), Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            sysLoginLogPO.setLoginStatus(Constants.SUCCESS);
        } else if (Constants.LOGIN_FAIL.equals(sysLoginLogDTO.getLoginStatus())) {
            sysLoginLogPO.setLoginStatus(Constants.FAIL);
        }
        sysLoginLogPO.setLoginTime(DateUtils.getDateTime());
        sysLoginLogDAO.insert(sysLoginLogPO);
    }

    @Override
    public PageDataInfo<SysLoginLogDTO> listPage(SysLoginLogDTO sysLoginLogDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysLoginLogDTO);
        // 拼接查询条件
        QueryWrapper<SysLoginLogPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<SysLoginLogPO> list = sysLoginLogDAO.selectList(queryWrapper);
        PageInfo<SysLoginLogPO> pageInfo = new PageInfo<>(list);
        PageDataInfo<SysLoginLogDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, SysLoginLogDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        return pageDataInfo;
    }
}
